package com.jzo2o.orders.manager.Handler.OrdersHandler;

import cn.hutool.core.util.RandomUtil;
import com.jzo2o.api.customer.AddressBookApi;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.manager.model.dto.request.PlaceOrderReqDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

import static com.jzo2o.orders.base.enums.OrderStatusEnum.NO_PAY;

/**
 * 订单填充处理Handler
 */
@Slf4j
@Component
@Order(20)  // 第二个执行
@RequiredArgsConstructor
public class OrderFillHandlerNew extends OrdersHandlerNew {

    private final String ORDER_NO_FORMAT = "%s%08d%02d%03d%02d";
    // 使用构造器注入，替代SpringUtil
    private final RedisTemplate redisTemplate;
    private final ServeApi serveApi;
    private final AddressBookApi addressBookApi;



    @Override
    public void process(Orders order, PlaceOrderReqDTO reqDTO) {
        log.info("订单填充处理Handler");
        
        // 1. 查询服务相关信息
        ServeAggregationResDTO serve = serveApi.findById(reqDTO.getServeId());
        BeanUtils.copyProperties(serve, order);
        // 设置服务id
        order.setServeId(reqDTO.getServeId());
        // 设置订单id
        order.setId(generateOrderId());

        // 2. 根据地址id查询地址信息
        AddressBookResDTO address = addressBookApi.getAddressById(reqDTO.getAddressBookId());
        // 设置地址详细信息
        String addressDetail = address.getProvince() + address.getCity() + address.getCounty() + address.getAddress();
        order.setServeAddress(addressDetail);
        // 设置经纬度
        order.setLocation(address.getLon() + "," + address.getLat());
        // 设置联系人信息
        order.setContactsName(address.getName());
        // 设置联系人手机号
        order.setContactsPhone(address.getPhone());

        // 3. 其他信息设置
        // 设置订单状态
        order.setOrdersStatus(OrderStatusEnum.NO_PAY.getStatus());
        // 设置支付状态
        order.setPayStatus(NO_PAY.getStatus());
        // 设置服务开始时间
        order.setServeStartTime(reqDTO.getServeStartTime());
        // 设置购买数量
        order.setPurNum(reqDTO.getPurNum());
        // 设置用户id
        order.setUserId(UserContext.currentUserId());
    }

    /**
     * 生成订单号逻辑
     * 17位：2位年+8位序号+2位月+3位随机数+2位日序号每月重置
     */
    private Long generateOrderId() {
        LocalDate now = LocalDate.now();
        String year = String.valueOf(now.getYear());
        int monthValue = now.getMonthValue();
        int day = now.getDayOfMonth();
        
        // 拼接key,用于保证订单号唯一,每个月重置
        String key = "ORDER_SEQ:" + year + monthValue;
        // 拿到8位序号
        Long increment = redisTemplate.opsForValue().increment(key);

        return Long.parseLong(String.format(ORDER_NO_FORMAT, year.substring(2), increment, monthValue,
                                           RandomUtil.randomInt(100, 999), day));
    }
    
    @Override
    public int getOrder() {
        return 20;
    }
}